//=============================================================================
// XBus 
//-----------------------------------------------------------------------------
// File        : info.c
// Description : 
// Author      : Ismail Sabry
// Date        : 1/30/2009
//=============================================================================
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#include <xbus_module.h>
#include <xbus_cell.h>
#include <xbus_interface.h>

int get_option(int argc, char **argv, char *xbus_name);
int xbus_scan(t_XBUS_interface *interface);

static int alive=1;

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void catch_signal(int sig)
{
  alive = 0;
  printf("Signal % d \n", sig);
  fflush(stdout);
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int main (int argc, char **argv)
{
  char               xbus_name[XBUS_SIZE_NAME];
  int                xbus_id=0;
  t_XBUS_interface   interface;

#if 0
  snprintf(xbus_name, XBUS_SIZE_NAME-1, "test");
#else
  if( get_option(argc, argv, xbus_name) == -1)
    return -1;
#endif

  signal(SIGINT,  catch_signal);
  signal(SIGQUIT, catch_signal);
  signal(SIGTERM, catch_signal);

  if( XBUS_link(&interface, XBUS_CONSUMER, xbus_name, xbus_id) == -1)
    return -1;
  
  xbus_scan(&interface);

  XBUS_unlink(&interface);
  
  return 0;

}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int get_option(int argc, char **argv, char *xbus_name)
{
  int ret=0;
  int c;
  
  opterr = 0;
  
  if(argc != 3) {
    ret = -1;
  } else {
    while ((c = getopt (argc, argv, "n:?")) != -1) {
      switch (c) {
      case 'n': 
	// xbus name
	snprintf(xbus_name, XBUS_SIZE_NAME-1, "%s", optarg);
	break;
      case '?':
      default :
	ret = -1;
	break;
      }
    }
  }
  
  if(ret != 0) {
    printf("\n");
    printf(" syntax: %s -n <name> \n", argv[0]);
    printf("    -n  xbus name\n");
    printf("    -?  this message \n");
    printf("\n");
  }

  return(ret);
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int xbus_scan(t_XBUS_interface *interface)
{
  int               i, j;
  t_XBUS_module     *module;
  t_XBUS_cell       *cell;
  
  printf("XBUS magic ........ : %X \n", (int)interface->xbus->head.magictag);
  printf("XBUS version ...... : %d.%d.%d \n", 
	 interface->xbus->head.version.major,
	 interface->xbus->head.version.minor, 
	 interface->xbus->head.version.change);
  printf("XBUS Name ......... : %s \n", interface->xbus->head.name);
  printf("XBUS Nb modules ... : %d \n", interface->xbus->head.nb_module);
  printf("XBUS Nb cells ..... : %d \n", interface->xbus->head.nb_cell);
  printf("XBUS Size Cell .... : %d \n", interface->xbus->head.size_cell);
  printf("XBUS Size ......... : %d \n", interface->xbus->head.size);
  
  for(i=0; i<interface->xbus->head.nb_module; i++) {
    module = XBUS_get_module_by_index(interface->xbus, i);
    
    printf(" Module(%d) [%X] iter %d / icell %d \n",i, (int)module->head.magictag, 
	   module->head.witer, module->head.wcell );
    
    cell = &module->cell;
    for(j=0; j<interface->xbus->head.nb_cell; j++) {
      if(cell->head.magictag != XBUS_MAGICTAG_CELL)
	printf(" Bad Cell(%d/%d) [%X] %d\n", i, j, (int)cell->head.magictag, (int)cell->head.len);
      cell = XBUS_get_next_cell(cell);
    }
  }

  return(0);
}
